#include <iostream>
#include <atomic>
#include <thread>
#include <assert.h>
std::atomic<bool> x, y;
std::atomic<int> z;
void write_x() // a
{
    x.store(true, std::memory_order_seq_cst);
}
void write_y() // b
{
    y.store(true, std::memory_order_seq_cst);
}
void read_x_then_y() // c
{
    while (!x.load(std::memory_order_seq_cst))
        ;                                  //  1
    if (y.load(std::memory_order_seq_cst)) // 2
        ++z;
}
void read_y_then_x() // d
{
    while (!y.load(std::memory_order_seq_cst))
        ;                                  // 3
    if (x.load(std::memory_order_seq_cst)) // 4
        ++z;
}
int main()
{
    x = false;
    y = false;
    z = 0;
    std::thread a(write_x);
    std::thread b(write_y);
    std::thread c(read_x_then_y);
    std::thread d(read_y_then_x);
    a.join();
    b.join();
    c.join();
    d.join();
    std::cout << "x:" << x << std::endl;
    std::cout << "y:" << y << std::endl;
    std::cout << "z:" << z << std::endl;
    assert(z.load() != 0);
}